package cn.cocho.custom.yunCashierDf.quartz;

import cn.cocho.custom.yunCashierDf.common.controller.EncryptCode;
import cn.cocho.custom.yunCashierDf.common.controller.HttpSubmit;
import cn.cocho.custom.yunCashierDf.merchant.domain.MerchantInfo;
import cn.cocho.custom.yunCashierDf.merchant.domain.MerchantTransactionInfo;
import cn.cocho.custom.yunCashierDf.merchant.service.MerchantInfoService;
import cn.cocho.custom.yunCashierDf.merchant.service.MerchantTransactionInfoService;
import cn.cocho.custom.yunCashierDf.utils.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by WQJ on 2017-06-02.
 */
public class QuartzJob {

    @Autowired
    private MerchantInfoService merchantInfoService;

    @Autowired
    private MerchantTransactionInfoService merchantTransactionInfoService;

    public void work() {
        System.out.println("进入定时任务");
        Long notTime = System.currentTimeMillis();
        System.out.println(notTime);
        List<MerchantTransactionInfo> listTransaction = merchantTransactionInfoService.getTimeOutTransactionInfo();
        for(int i = 0;i < listTransaction.size() ; i ++){
            //先发送查询请求，查询结果如果是未支付，就进行取消订单
            System.out.println(listTransaction.get(i).getMerchantId());
            System.out.println(listTransaction.get(i).getOrigOrderNum());
            System.out.println(listTransaction.get(i).getTerminalid());
            System.out.println("第"+i+"次进入循环");
            MerchantInfo merchantInfo = merchantInfoService.getMerchantInfoById(listTransaction.get(i).getMerchantId());
            if(merchantInfo != null){
                //查询接口
                String busicd = "INQY";
                String charset = merchantInfo.getCharset();
                String inscd = merchantInfo.getInscd();
                String mchntid = merchantInfo.getMchntid();
                String origOrderNum = listTransaction.get(i).getOrigOrderNum();  //使用进行过预下单的订单号进行查询
                String signType = merchantInfo.getSignType();
                String terminalid = listTransaction.get(i).getTerminalid();
                String txndir = merchantInfo.getTxndir();
                String version = merchantInfo.getVersion();

                String newString = "busicd="+busicd+"&charset="+charset+"&inscd="+inscd+"&mchntid="+mchntid+"&origOrderNum="+origOrderNum+""
                        + "&signType="+signType+"&terminalid="+terminalid+"&txndir="+txndir+"&version="+version+"";

                String newSign = newString + merchantInfo.getSignKey();
                String sign = new EncryptCode().SHA256(newSign);

                JSONObject jsonObj = new JSONObject();
                jsonObj.put("txndir", txndir);
                jsonObj.put("busicd", busicd);
                jsonObj.put("inscd", inscd);
                jsonObj.put("charset", charset);
                jsonObj.put("mchntid", mchntid);
                jsonObj.put("signType", signType);
                jsonObj.put("terminalid", terminalid);
                jsonObj.put("origOrderNum", origOrderNum);
                jsonObj.put("version", version);
                jsonObj.put("sign", sign);

                String jsonString = jsonObj.toString();

                //获取返回值
                String returnValue = new HttpSubmit().postAttendanceInfo(jsonString, StringUtil.requestUrl);
                System.out.println("返回查询结果"+returnValue);
                JSONObject jsonObject = JSONObject.fromObject(returnValue);

                if(jsonObject.getString("respcd").equals("09")){
                    //超时支付 取消订单
                    MerchantTransactionInfo merchantTransactionInfo = merchantTransactionInfoService.getQuMerchantTransactionInfo(merchantInfo.getMerchantId(), origOrderNum);
                    if(merchantInfo != null && merchantTransactionInfo != null){
                        String cancBusicd = "CANC";
                        String orderNum = merchantTransactionInfo.getOrderNum();
                        String cancNewString = "busicd="+cancBusicd+"&charset="+charset+"&inscd="+inscd+"&mchntid="+mchntid+"&orderNum="+orderNum+"&origOrderNum="+origOrderNum+""
                                + "&signType="+signType+"&terminalid="+terminalid+"&txndir="+txndir+"&version="+version+"";
                        String cancnewSign = cancNewString + merchantInfo.getSignKey();
                        String cancSign = new EncryptCode().SHA256(cancnewSign);

                        JSONObject cancJsonObj = new JSONObject();
                        cancJsonObj.put("txndir", txndir);
                        cancJsonObj.put("busicd", cancBusicd);
                        cancJsonObj.put("inscd", inscd);
                        cancJsonObj.put("charset", charset);
                        cancJsonObj.put("mchntid", mchntid);
                        cancJsonObj.put("signType", signType);
                        cancJsonObj.put("terminalid", terminalid);
                        cancJsonObj.put("orderNum", orderNum);
                        cancJsonObj.put("origOrderNum", origOrderNum);
                        cancJsonObj.put("version", version);
                        cancJsonObj.put("sign", cancSign);
                        String cancJsonString = cancJsonObj.toString();
                        //获取返回值
                        String cancReturnValue = new HttpSubmit().postAttendanceInfo(cancJsonString, StringUtil.requestUrl);
                        System.out.println("取消订单时返回结果"+cancReturnValue);
                        //解析json数据
                        JSONObject cancJsonObject = JSONObject.fromObject(cancReturnValue);
                        merchantTransactionInfo.setSearchStatus("1");
                        merchantTransactionInfo.setBusicd(cancJsonObject.getString("busicd"));
                        merchantTransactionInfo.setRespcd(cancJsonObject.getString("respcd"));
                        merchantTransactionInfoService.replaceMerchantTransactionInfo(merchantTransactionInfo);
                        System.out.println("成功取消订单号为"+listTransaction.get(i).getOrigOrderNum()+"的订单");
                    }else{
                        System.out.print("查询出错，参数错误！");
                    }
                }else{
                    MerchantTransactionInfo merchantTransactionInfo = merchantTransactionInfoService.getQuMerchantTransactionInfo(merchantInfo.getMerchantId(),origOrderNum);
                    merchantTransactionInfo.setSearchStatus("1");
                    merchantTransactionInfo.setBusicd(jsonObject.getString("busicd"));
                    merchantTransactionInfo.setRespcd(jsonObject.getString("respcd"));
                    if(merchantTransactionInfo.getRespcd().equals("00")){
                        merchantTransactionInfo.setChannelOrderNum(jsonObject.getString("channelOrderNum"));
                    }
                    merchantTransactionInfoService.replaceMerchantTransactionInfo(merchantTransactionInfo);
                }
            }else{
                System.out.print("查询出错，参数错误！");
            }
        }
    }
}
